/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectUnit;
import DTO.Permission_DTO;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Nguyen Phi Hung
 */
public class Permission_BLL {
    ConnectUnit connection;
    
    public Permission_BLL(){
        this.connection = DAL.DAL.getDAL();
    }
    
    public HashSet<Permission_DTO> getPermissions(String condition, String OrderBy) throws Exception{
        ResultSet result = this.connection.Select("PERMISSION", condition, OrderBy);
        HashSet<Permission_DTO> permissions = new HashSet<Permission_DTO>();
        while(result.next()){
            Permission_DTO permission = new Permission_DTO();
            
            permission.setPermissionID(result.getString("PermissionID"));
            permission.setPermissionName(result.getString("PermissionName"));
            permission.setPermission(result.getBoolean("Permission"));
            permission.setDeleteInfo(result.getBoolean("DeleteInfo"));
            permission.setDepositWithdraw(result.getBoolean("DepositWithdraw"));
            permission.setReport(result.getBoolean("Report"));
            permission.setSearch(result.getBoolean("Search"));
            
            permissions.add(permission);
        }
        return permissions;
    }
    
    public HashSet<Permission_DTO> getPermissions(String condition) throws Exception{
        return this.getPermissions(condition, null);
    }
    
    public HashSet<Permission_DTO> getPermissions() throws Exception{
        return this.getPermissions(null, null);
    }
    
    public Permission_DTO getPermission_byPermissionID(String ID) throws Exception{
        HashSet<Permission_DTO> permission = this.getPermissions("PermissionID="+ ID);
        if(permission.size()>=0)
           return permission.toArray(new Permission_DTO[permission.size()])[0];
        return null;     
    }
    
    public void Insert(Permission_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("PermissionID", info.getPermissionID());
        map.put("PermissionName", info.getPermissionName());
        map.put("Permission", info.isPermission());
        map.put("DeleteInfo", info.isDeleteInfo());
        map.put("DepositWithdraw", info.isDepositWithdraw());
        map.put("Report", info.isReport());
        map.put("Search", info.isSearch());
        
        String[] arr = {"PermissionID","PermissionName","Permission","DeleteInfo","DepositWithdraw","Report","Search",};
        this.connection.Insert("PERMISSION", map, arr);
    }
    public void Update(Permission_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("PermissionID", info.getPermissionID());
        map.put("PermissionName", info.getPermissionName());
        map.put("Permission", info.isPermission());
        map.put("DeleteInfo", info.isDeleteInfo());
        map.put("DepositWithdraw", info.isDepositWithdraw());
        map.put("Report", info.isReport());
        map.put("Search", info.isSearch());
        this.connection.Update("PERMISSION", map, "PermissionID="+ info.getPermissionID());
    }
    public void Delete(Permission_DTO info) throws Exception{
        this.connection.Delete("PERMISSION",  "PermissionID="+ info.getPermissionID());
    }
}
